﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using DevExpress.Web.ASPxGridView;
using System.Data;

public partial class Business_BookHisQuery : Frame.Pages.BasePage

{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender,e);
        Frame.Global.GlobalFunctions.SetGridView(this.gridView, false);
        if (!IsPostBack && !IsCallback)
        {
            string sqlstr = "select PeriodName=a.PeriodName + ' ' + BeginTime+'——'+EndTime,a.PeriodID from BI_ConsumePeriod a";
            using (SqlDataReader dr = Frame.Global.GlobalSqlHelper.GetSQLDataReader(sqlstr))
            {
                for (; dr.Read(); )
                {
                    Query_Period.Items.Add(dr["PeriodName"].ToString(), dr["PeriodID"]);
                }
            }
            //Frame.Global.GlobalFunctions.InitComboBox(Query_Period, "BI_ConsumePeriod", "PeriodName ", "PeriodID");
            Query_Period.Items.Insert(0,new DevExpress.Web.ASPxEditors.ListEditItem("全部时段",-1));
            Query_Period.SelectedIndex = 0;
            Query_BeginT.Date = DateTime.Today;
            Query_EndT.Date = DateTime.Today;
            Session["sqlstr"] = null;
            Session["sqlParams"] = null;
        }

        DDataBind();
    }
    DataTable table;
    public DataTable Table
    {
        get
        {
            if (Session["sqlstr"] != null && Session["sqlParams"] != null)
            {
                table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(Session["sqlstr"].ToString(), Session["sqlParams"]as SqlParameter[]);
            }
            return table;
        }
    }
    void DDataBind()
    {
        if (Session["sqlstr"] != null && Session["sqlParams"] !=null)
        {
            gridView.DataSource = Table;
            gridView.DataBind();
        }
    }
    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (Query_EndT.Date.Subtract(Query_BeginT.Date).TotalDays>10)
        {
            throw new Exception("时间间隔不可大于十天！");
        }
        string SqlstrFormat = @"     SELECT	Book.BookID,
		                            Period.PeriodName,
		                            Book.Consumer_ID,
		                            Book.BookDate,
		                            Book.AddTime,
		                            Book.BookNo,
		                            Book.[Money],
		                            Book.BookType,
                                    Book.ContractName,
                                    Book.ContractPhoneNum
                            FROM	dbo.BLI_Book AS Book,
	                                dbo.BI_ConsumePeriod AS Period
                            WHERE   {0} Book.Perid_ID=Period.PeriodID";
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        List<SqlParameter> Params = new List<SqlParameter>();
        if (!string.IsNullOrEmpty(Query_BeginT.Text.Trim()))
        {
            sb.Append("Book.BookDate>=@bookBeginDate AND ");
            Params.Add(new SqlParameter("@bookBeginDate", Query_BeginT.Date));
        }
        if (!string.IsNullOrEmpty(Query_EndT.Text.Trim()))
        {
            sb.Append("book.BookDate<@bookEndDate AND ");
            Params.Add(new SqlParameter("@bookEndDate", Query_EndT.Date.AddDays(1)));
        }
        if (!string.IsNullOrEmpty(Query_BookNo.Text.Trim()))
        {
            sb.Append("book.BookNo like '%'+@BookNo+'%' AND ");
            Params.Add(new SqlParameter("@BookNo", Query_BookNo.Text.Trim()));
        }
        if (!string.IsNullOrEmpty(Query_ContractName.Text.Trim()))
        {
            sb.Append("book.ContractName like '%'+@ContractName+'%' AND ");
            Params.Add(new SqlParameter("@ContractName", Query_ContractName.Text.Trim()));
        }
        if (!string.IsNullOrEmpty(Query_ContractPhoneNum.Text.Trim()))
        {
            sb.Append("book.ContractPhoneNum = @ContractPhoneNum AND ");
            Params.Add(new SqlParameter("@ContractPhoneNum", Query_ContractPhoneNum.Text.Trim()));
        }
        if (Query_Period.SelectedIndex != 0)
        {
            sb.Append("book.Perid_ID = @Perid_ID AND ");
            Params.Add(new SqlParameter("@Perid_ID", Convert.ToInt32(Query_Period.Value)));
        }
        Session["sqlstr"] = string.Format(SqlstrFormat, sb.ToString());
        Session["sqlParams"] = Params.ToArray();
        gridView.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlstrFormat, sb.ToString()),Params.ToArray());
        gridView.DataBind();
    }
    protected void ConsumerPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (!string.IsNullOrEmpty(e.Parameter))
        {
            using (DAL.CMSDALDataContext Dcontext = new DAL.CMSDALDataContext())
            {
                DAL.BI_Consumer model = Dcontext.BI_Consumer.FirstOrDefault(o => o.ConsumerID.ToString() == e.Parameter);
                WebUtility.MappingConsumerView(ConsumerPanel, model);
            }
        }
    }
    protected void DetailgridView_Load(object sender, EventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;
        Frame.Global.GlobalFunctions.SetGridView(grid, false);
        string BookNo = grid.GetMasterRowFieldValues("BookNo").ToString();
        string SqlstrFormat = @"SELECT  Area.AreaName,
		                                Detail.BookNo,
		                                Detail.Remark,
		                                Detail.State,
		                                Tables.TableID,
		                                Tables.TableName,
		                                Tables.PCount
                                FROM	dbo.BLI_BookDetail AS Detail,
	                                dbo.BI_Tables AS Tables,
	                                dbo.BI_Area AS Area
                                WHERE	Area.AreaID=Tables.Area_ID AND
	                                Tables.TableID = Detail.Table_ID AND 
	                                Detail.BookNo='{0}'";
        grid.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlstrFormat, BookNo));
        grid.DataBind();
    }

    protected override string ActionID
    {
        get { return "0"; }
    }
}